#encoding=utf-8
import os
import subprocess

gSpecialSymbolsTup = ('：', '、', '（', '>', '》', '，', '；', '。') # 分割符号
gTruncationSymbolList = ['（', '《', '<', '▪', '/'] # 替换符号

class TextProcessingSystem:
    isRemoveSrcFile = False
    def __init__(self):
        pass
    def TextProcessingSystem(self):
        pass
    def HandleSymbolsInFiles(self, fileName, isRemoveSrcFile=False):
        srcFile = fileName
        path = os.path.split(srcFile)
        file = path[1].split('.')
        desFile = os.path.join(path[0], file[0] + 'Tmp.txt')
        # copyFile = os.path.join(path[0], file + 'Copy.txt')
        srcFileFp = open(srcFile, mode='r', encoding='utf-8');
        desFileFp = open(desFile, mode='w+', encoding='utf-8');
        # copyFileFp = open(desFile, mode='w+', encoding='utf-8');
        for line in srcFileFp.readlines():
            # 去除空格/制表符/
            line = line.strip().rstrip().replace('\t', '')
            if len(line):
                # 将英文字符全部替换为中文字符
                line = line.replace(',', '，').replace('.', '。').replace('!', '！').replace('!', '！')
                line = line.replace(' ', '').replace('(', '（').replace(')', '）').replace('[', '【').replace(']', '】')
                line = line.replace('?', '？') + line.replace('"', '') + '\n'
                desFileFp.write(line)
                # print(line)
        srcFileFp.close()
        desFileFp.close()
        if isRemoveSrcFile:
            os.remove(srcFile)
            os.rename(desFile, srcFile)

    def RemoveSpecialCharacter(self, str):
        for chr in str:
            if ((gTruncationSymbolList.count(chr) != 0) or (chr.isdigit())):
                str = str.replace(chr, '')
        return str

    def TruncationStringBySpecialSymbols(self, str):
        for ch in gSpecialSymbolsTup:
            if str.count(ch):
                str = str.split(ch)[0]
        return str

    def GetDict2BaiduEncyclopedia(self, filename, myDictFile):
        # 载入自定义字典
        with open(myDictFile, mode='r', encoding='utf-8') as fp:
            dictList = fp.readlines()
        # 移除dictList中的空字段
        while dictList.count('\n'):
            dictList.remove('\n')
        resultList = []
        srcFileFp = open(filename, mode='r', encoding='utf-8')
        desFileFp = open(myDictFile, mode='a+', encoding='utf-8')
        for line in srcFileFp.readlines():
            line = self.TruncationStringBySpecialSymbols(line)
            line = self.RemoveSpecialCharacter(line)
            line = line.replace('\n', '') + '\n'

            if ((dictList.count(line) == 0) and (resultList.count(line) == 0)):
                line = self.RemoveSpecialCharacter(line)
                if (len(line) > 8) or (line == '\n'):
                    continue
                resultList.append(line)
                # print(line)
        desFileFp.writelines(resultList)
        srcFileFp.close()
        desFileFp.close()

    # keywordsFilePath 存放素材字典文件夹
    # novelName 小说名字
    # recordLineNum 记录素材的行数
    # materialPath  # 结果目录
    def ExtractMaterialByKeyworld(self, keywordsFilePath, novelName, recordLineNum, materialPath):
        print('Extract Material By Keyworld Begin.')
        if not os.path.exists(keywordsFilePath):
            print('keywordsFilePath is not exists!')
            return
        if not os.path.exists(materialPath):
            os.makedirs(materialPath)
        with open(novelName, mode='r', encoding='utf-8') as fp:
            lines = fp.readlines()
        for keywordsFile in os.listdir(keywordsFilePath):
            keywordsFile = os.path.join(keywordsFilePath, keywordsFile)
            keywordsLabel = os.path.basename(keywordsFile).split('.')[0]
            print('%s_素材开始提取' % keywordsLabel)
            materialFile = os.path.join(materialPath, '&素材_' + keywordsLabel + '.txt')
            materialFileFp = open(materialFile, mode='w', encoding='utf-8')
            with open(keywordsFile, mode='r', encoding='utf-8') as keywordsHandle:
                keywords = keywordsHandle.readlines()
            cnt = 0
            keywordCnt = 1
            totalLineNum = len(lines)
            for i in range(0, totalLineNum):
                line = lines[i].strip()
                if (cnt > totalLineNum):
                    break
                if (len(line) == 0) or (i < cnt):
                    continue
                if (line.find('第') != -1) and (line.find('章') != -1) and (len(line) < 10):
                    # print(line)  # 写入
                    materialFileFp.write(line + '\n')
                for keyword in keywords:
                    if (line.find(keyword.strip()) != -1) and (len(keyword.strip()) > 0):
                        # print('%s_%d' % (keywordsLabel, keywordCnt))  # 写入
                        materialFileFp.write('&素材_%s_%d\n' % (keywordsLabel, keywordCnt))
                        for j in range(i, i + recordLineNum):
                            cnt = j
                            if (j < totalLineNum):
                                # print(lines[j])  # 写入
                                materialFileFp.write(lines[j].strip() + '\n')
                        keywordCnt = keywordCnt + 1
                        break
            materialFileFp.close()
            print('%s_素材提取完成' %keywordsLabel)
        print('Extract Material By Keyworld End.')

    def ExtractDialogue(self, novelName, materialPath):
        materialFile =  os.path.join(materialPath, '对话_素材.txt')
        materialFileFp = open(materialFile, mode='w', encoding='utf-8')
        with open(novelName, mode='r') as fp:
            for line in fp.readlines():
                if (line.find('第') != -1) and (line.find('章') != -1) and (len(line) < 10):
                    # print(line)  # 写入
                    materialFileFp.write(line + '\n')
                if ((line.find('“') != -1) and (line.find('”') != -1)) or ((line.find('"') != -1)):
                    # 写入
                    materialFileFp.write(line + '\n')
        materialFileFp.close()

if __name__ == '__main__':
    textProcessingSystem = TextProcessingSystem()
    novelName = "F:/111/精品小说/遮天.txt" # 小说名字
    recordLineNum = 10 # 记录素材的行数
    materialPath = 'G:\\result\\' # 结果目录
    keywordsFilePath = 'G:\PredatorBrainLite\\'
    # textProcessingSystem.ExtractMaterialByKeyworld(keywordsFilePath, novelName, recordLineNum, materialPath)
    textProcessingSystem.ExtractDialogue(novelName, materialPath)